#include "TcpServer.hpp"
#include "Protocol.hpp"

#include <iostream>
#include <memory>

using namespace std;
using namespace tcpserver_ns;
using namespace protocol_ns;

Response caculator(const Request &rt)
{
    cout<<rt._x<<rt._op<<rt._y<<endl;
    Response rs(0, 0);
    switch (rt._op)
    {
    case '+':
        rs._result = rt._x + rt._y;
        break;
    case '-':
        rs._result = rt._x - rt._y;
        break;
    case '*':
        rs._result = rt._x * rt._y;
        break;
    case '/':
        if (rt._y == 0)
        {
            rs._code = 1;
            return rs;
        }
        rs._result = rt._x / rt._y;
        break;
    case '%':
        if (rt._y == 0)
        {
            rs._code = 2;
            return rs;
        }
        rs._result = rt._x % rt._y;
        break;
    default:
        rs._code=3;
    }
    return rs;
}

static void Usage(std::string proc)
{
    std::cout << "Usage:\n\t" << proc << " port\n"
              << std::endl;
}

int main(int argc,char* argv[])
{
    if(argc!=2)
    {
        Usage(argv[1]);
        exit(USE_ERR);
    }
    uint16_t port=atoi(argv[1]);
    unique_ptr<tcp_server> ts(new tcp_server(caculator,port));
    ts->Init();
    ts->Start();
    return 0;
}